//
//  DynamicViewController.m
//  MJApp
//
//  Created by wangliang on 15/5/27.
//  Copyright (c) 2015年 wangliang. All rights reserved.
//

#import "DynamicViewController.h"

#import "DynamicOneCollectionViewCell.h"
#import "DynamicTwoCollectionViewCell.h"
#import "DynamicThreeCollectionViewCell.h"
#import "HomeBannerOneCollectionViewCell.h"

#import "DynamicSendView.h"
#import "PhotoModel.h"
#import "DynamicMoreView.h"
#import "WLAlertView.h"
#import "MineRainViewController.h"
#import "WLJSON.h"
#import "DynamicSendViewController.h"
#import "LoginAndRegisterViewController.h"
#import "ShareModel.h"
#import "LoginViewController.h"

@interface DynamicViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,
    UICollectionViewDelegateFlowLayout, UIActionSheetDelegate,HomeCellDelegate,
    UIImagePickerControllerDelegate,UINavigationControllerDelegate,UMSocialUIDelegate,WXApiDelegate>

@property (nonatomic,strong) DynamicSendView *sendView;
@property (nonatomic, strong) UIActionSheet *actionSheet;
@property (nonatomic, strong) DynamicMoreView *moreView;
@property (nonatomic,strong) NSMutableArray *tableArray;
@property (nonatomic, strong) UICollectionView *collectionView;
@property (nonatomic, strong) NSDictionary *trendDic;


@property(nonatomic,strong) NSArray *pullArr;
@property(nonatomic,strong) NSArray *bannerArr;

@property(nonatomic,strong) NSDictionary *infoDic;//个人中心的动态
@property(nonatomic,strong)UMSocialIconActionSheet *shareSheet;

@end

@implementation DynamicViewController

- (instancetype)initWithDic:(NSDictionary *)dic {
    
    if (self = [super init]) {
        
        self.title = @"旅行圈";
//        if (dic[@""]) {
//            <#statements#>
//        }
        self.infoDic = dic;
    }
    return self;
}

- (NSMutableArray *)tableArray {
    
    if (!_tableArray) {
        
        _tableArray = [NSMutableArray array];
    }
    return _tableArray;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self UI];
    [self NaviUI];
    if (!self.infoDic) {
        
        [self obationTopic];
        [self obationBanner];
    }
    
    if ([self.title isEqual:@"我的动态"]) {
        
        self.moreView.state = DynamicMoreViewStateSelf;
    }else {
        self.moreView.state = DynamicMoreViewStateOther;
    }
}


- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self obationData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (DynamicMoreView *)moreView {
    
    if (!_moreView) {
        _moreView = [[DynamicMoreView alloc] init];
        _moreView.delegate = self;
    }
    return _moreView;
}

- (void)UI {
   
    
    
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
    flowLayout.sectionInset = UIEdgeInsetsMake(10, 20, 10, 20);
    
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.size.height - 64)
                                                          collectionViewLayout:flowLayout];
    collectionView.backgroundColor = [UIColor colorWithWhite:1 alpha:0.9];
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    
    UINib *oneNib = [UINib nibWithNibName:@"DynamicOneCollectionViewCell" bundle:nil];
    UINib *twoNib = [UINib nibWithNibName:@"DynamicTwoCollectionViewCell" bundle:nil];
    UINib *threeNib = [UINib nibWithNibName:@"DynamicThreeCollectionViewCell" bundle:nil];
    UINib *fourNib = [UINib nibWithNibName:@"HomeBannerOneCollectionViewCell" bundle:nil];
    
    [collectionView registerNib:oneNib forCellWithReuseIdentifier:@"DynamicOneCollectionViewCell"];
    [collectionView registerNib:twoNib forCellWithReuseIdentifier:@"DynamicTwoCollectionViewCell"];
    [collectionView registerNib:threeNib forCellWithReuseIdentifier:@"DynamicThreeCollectionViewCell"];
    [collectionView registerNib:fourNib forCellWithReuseIdentifier:@"HomeBannerOneCollectionViewCell"];
    self.collectionView = collectionView;
    
}


- (void)NaviUI {
    
    if (![self.title isEqual:@"TA的动态"]) {
        
        [self.rightBtn setImage:[UIImage imageNamed:@"dt_03"] forState:UIControlStateNormal];
    }
}

- (void)clickRightButton {

    if (![self.title isEqual:@"TA的动态"]) {
        if(![userInfoTool share].ID) {
            
            [self presentViewController:[[LoginViewController alloc] init] animated:YES completion:nil];
            return;
        }
        [self.navigationController pushViewController:[[DynamicSendViewController alloc] init] animated:YES];
    }
}

- (void)obationData {
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    if (self.infoDic) {
        dic[@"user_id"] = self.infoDic[@"id"];
    }
    [WLHttpRequestAfter userGetTrendWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {

        if ([resultDictionary[@"status"] integerValue] == 1) {
        
            [self.tableArray removeAllObjects];
            [self.tableArray addObjectsFromArray:resultDictionary[@"data"]];
            [self.collectionView reloadData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
    } andFailureBlock:^(NSError *resultError) {
        
        [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultError.description];
    }];
    
}

- (void)obationTopic {
    
    [WLHttpRequestAfter userGetTopicWithParam:nil usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
           
            self.pullArr = resultDictionary[@"data"];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:1 inSection:0];
            [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

- (void)obationBanner {
    
    [WLHttpRequestAfter bannerGetBannerWithParam:@{@"type":@"1"} usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        
        
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            self.bannerArr = resultDictionary[@"data"];
            NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
            [self.collectionView reloadItemsAtIndexPaths:@[indexPath]];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}

#pragma mark collectionViewDelegate


- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    
    return 1;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    if (self.infoDic) {
        
        return self.tableArray.count;
    }else {
        
        return 2 + self.tableArray.count;
    }
    
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    
    CGSize size = CGSizeZero;
    size.width = IOS_IPHONE_WINDOW_WIDE - 20;
    if (!self.infoDic) {
        
        if (indexPath.row == 0) {
            
            size.height = 90;
        }else if (indexPath.row == 1) {
            
            size.height = 90;
        }else {
            
            if ([self.tableArray[indexPath.row - 2][@"images"] count] > 0) {
                
                size.height = 250;
            }else {
                size.height = 250 - 80;
            }
        }
    }else {
        if ([self.tableArray[indexPath.row][@"images"] count] > 0) {
            
            size.height = 250;
        }else {
            size.height = 250 - 80;
        }

    }
    return size;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    if (!self.infoDic) {
        
        if (indexPath.row == 0) {
            
            HomeBannerOneCollectionViewCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HomeBannerOneCollectionViewCell" forIndexPath:indexPath];
            if (self.bannerArr) {
                
                oneCell.infoArr = self.bannerArr;
            }
            return oneCell;
        }else if (indexPath.row == 1) {
            
            DynamicTwoCollectionViewCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DynamicTwoCollectionViewCell" forIndexPath:indexPath];
            if (self.pullArr) {
                
                oneCell.infoArr = self.pullArr;
            }
            oneCell.delegate = self;
            return oneCell;
        }else {
            
            DynamicThreeCollectionViewCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DynamicThreeCollectionViewCell" forIndexPath:indexPath];
            NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.tableArray[indexPath.row - 2]];
            [dic setObject:@(indexPath.row - 2) forKey:@"index"];
            oneCell.infoDic = dic;
            oneCell.delegate = self;
            return oneCell;
        }
    }else {
        
        DynamicThreeCollectionViewCell *oneCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DynamicThreeCollectionViewCell" forIndexPath:indexPath];
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:self.tableArray[indexPath.row]];
        [dic setObject:@(indexPath.row) forKey:@"index"];
        oneCell.infoDic = dic;
        oneCell.delegate = self;
        return oneCell;
    }
}




- (void)homeCell:(id)cell withInfo:(id)info {
    
    if ([cell isKindOfClass:[DynamicSendView class]]) {
        
  
        return;
    }else if ([cell isKindOfClass:[DynamicThreeCollectionViewCell class]]) {
        
        self.trendDic = ((DynamicThreeCollectionViewCell *)cell).infoDic;
        NSInteger index = [info[@"index"] integerValue];
        if (index == 1) {
            
            [WLAlertView showWith:self.moreView withCenterStatus:CenterStatusDown];
        }else if (index == 2) {
            
            NSInteger row = [self.trendDic[@"index"] integerValue];
            [self.navigationController pushViewController:[[MineRainViewController alloc] initWithDic:self.tableArray[row]] animated:YES];
        }
        return;
    }else if([cell isKindOfClass:[DynamicMoreView class]]) {
        
        NSInteger index = [info[@"index"] integerValue];
        [WLAlertView dismiss];
        if (index == 1) {
            
            //删除
            [self deleteTrend];
        }else if (index == 2) {
//            
//            NSMutableArray *snsNames = [NSMutableArray array];
//            [snsNames addObject:UMShareToWechatSession];
//            [snsNames addObject:UMShareToWechatTimeline];
//            [snsNames addObject:UMShareToQQ];
//            [snsNames addObject:UMShareToQzone];
//            [snsNames addObject:UMShareToSina];
//            [UMSocialSnsService presentSnsIconSheetView:self
//                                                 appKey:UM_AppKey
//                                              shareText:@"你要分享的文字"
//                                             shareImage:[UIImage imageNamed:@"icon.png"]
//                                        shareToSnsNames:snsNames
//                                               delegate:self];
            
            NSMutableDictionary *dic = [NSMutableDictionary dictionary];
            dic[@"text"] = @"test";
//            dic[@"image"] = [UIImage imageNamed:@"1"]
            [ShareModel shareModelWithObject:self withDic:dic];
        }else if (index == 3) {
            
            
        }
        return;
    }else if ([cell isKindOfClass:[DynamicTwoCollectionViewCell class]]) {
        
        NSDictionary *dic = info;
        DynamicViewController *dyVC = [[DynamicViewController alloc] initWithDic:dic];
        dyVC.title = dic[@"title"];
        [self.navigationController pushViewController:dyVC animated:YES];
    }
}



- (void)deleteTrend {
    
    NSDictionary *dic = @{
                          @"id":self.trendDic[@"id"]
                          };
    [WLHttpRequestAfter userDelTrendWithParam:dic usingSucessBlock:^(BOOL isSucess, NSDictionary *resultDictionary) {
        if ([resultDictionary[@"status"] integerValue] == 1) {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:@"删除成功"];
            [self obationData];
        }else {
            
            [[DMCAlertCenter defaultCenter] postAlertWithMessage:resultDictionary[@"msg"]];
        }
    } andFailureBlock:^(NSError *resultError) {
        
    }];
}


-(void)didFinishGetUMSocialDataInViewController:(UMSocialResponseEntity *)response {
    
    if (response.responseCode == UMSResponseCodeSuccess) {
        
        
    }
}




@end
